#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename  Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

struct CompareList
{
    bool operator()( const int& lhs, const int& rhs ) const
    {
        cout << "struct CompareList" << endl;
        return lhs > rhs;
    }
};
void test()
{
    list<int> number = {1, 3, 5, 5, 6, 4, 8, 2, 3, 5};
    display(number);

    cout << endl << "list的去重" << endl;
    number.unique();
    display(number);

    cout << endl << "list的逆置" << endl;
    number.reverse();
    display(number);

    cout << endl << "list的sort操作" << endl;
    number.sort();
    /* std::less<int>  tmp; */
    /* number.sort(tmp); */
    /* number.sort(std::less<int>()); */
    /* number.sort(std::greater<int>()); */
    /* number.sort(CompareList()); */
    display(number);

    cout << endl << "list的逆置" << endl;
    number.reverse();
    display(number);

    cout << endl << "list的去重" << endl;
    number.unique();
    display(number);

    cout << endl << "list的合并操作" << endl;
    list<int> other = {11, 44, 33, 7, 66, 55};
    other.sort();
    display(other);
    number.sort();
    display(number);
    number.merge(other);
    display(number);
    display(other);

    cout << endl << "list的splice操作" << endl;
    list<int> number2 = {111, 333, 888, 777, 444};
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
#if 0
    number.splice(it, number2);
    display(number);
    display(number2);
#endif

    auto it2 = number2.begin();
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, other, it2);
    display(number);
    display(number2);

    cout << endl;
    it2 = number2.begin();
    auto it3 = number2.end();
    --it3;
    cout << "*it2 = " << *it2 << endl;
    cout << "*it3 = " << *it3 << endl;
    number.splice(it, other, it2, it3);
    display(number);
    display(number2);

    cout << endl << "splice在同一个链表中的操作" << endl;
    it = number.begin();
    ++it;
    ++it;
    it2 = number.end();
    --it2;
    --it2;
    --it2;
    it3 = number.end();
    --it3;
    cout << "*it = " << *it << endl;
    cout << "*it2 = " << *it2 << endl;
    cout << "*it3 = " << *it3 << endl;
    number.splice(it, number, it2, it3);
    display(number);

}


int main(int argc, char *argv[])
{
    test();
    return 0;
}

